package com.licq.tv.slice;

import com.licq.tv.ResourceTable;
import com.licq.tv.videoplayer.VideoView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.*;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class VideoplayerAbilitySlice extends AbilitySlice implements SurfaceOps.Callback {

    VideoView videoView;
    Image playBtn;
    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        //设置页面背景透明
        WindowManager windowManager = WindowManager.getInstance();
        Window window = windowManager.getTopWindow().get();
        window.setTransparent(true);

        //页面父布局
        DependentLayout myLayout = new DependentLayout(this);
        DependentLayout.LayoutConfig params = new DependentLayout.LayoutConfig(MATCH_PARENT, MATCH_PARENT);
        myLayout.setLayoutConfig(params);
        //显示视频的自定义videoView
        DependentLayout.LayoutConfig lpVideo = new DependentLayout.LayoutConfig(MATCH_PARENT, MATCH_PARENT);
        videoView = new VideoView(this, this);
        myLayout.addComponent(videoView, lpVideo);

        DependentLayout rlParent = new DependentLayout(this);
        DependentLayout.LayoutConfig lpParent = new DependentLayout.LayoutConfig(MATCH_PARENT, MATCH_PARENT);
        myLayout.addComponent(rlParent, lpParent);
        //显示播放暂停按钮
        playBtn = new Image(this);
        DependentLayout.LayoutConfig lpPlayBtn = new DependentLayout.LayoutConfig(40, 40);
        playBtn.setLayoutConfig(lpPlayBtn);
        playBtn.setId(1112);
        playBtn.setPixelMap(ResourceTable.Media_icon);
        playBtn.invalidate();
        playBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
//                if (videoView.getPlayState() == VideoView.STATE_PLAYING) {
//                    videoView.pause();
//                    playBtn.setPixelMap(ResourceTable.Media_play);
//                } else {
//                    videoView.start();
//                    playBtn.setPixelMap(ResourceTable.Media_pause);
//                }
            }
        });
        rlParent.addComponent(playBtn);
        super.setUIContent(myLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void surfaceCreated(SurfaceOps surfaceOps) {
        videoView.playAssets("resources/rawfile/video.mp4", true, surfaceOps);
    }

    @Override
    public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceOps surfaceOps) {

    }
}
